#include "stm32f10x.h"
#include "config.h"
#include "util.h"
#include "w5200.h"
#include <stdio.h>
#include <stdarg.h>

extern CONFIG_MSG Config_Msg;
extern u8 txsize[MAX_SOCK_NUM];
extern u8 rxsize[MAX_SOCK_NUM];


#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */



void Set_network(void)
{
	u8 ip[4];

	setSHAR(Config_Msg.Mac);
	setSUBR(Config_Msg.Sub);
	setGAR(Config_Msg.Gw);
	setSIPR(Config_Msg.Lip);

	setRTR(6000);
	setRCR(3);
	
	sysinit(txsize, rxsize);


	getSIPR (ip);
	printf("\r\nIP : %d.%d.%d.%d", ip[0],ip[1],ip[2],ip[3]);

	getSUBR(ip);
	printf("\r\nSN : %d.%d.%d.%d", ip[0],ip[1],ip[2],ip[3]);

	getGAR(ip);
	printf("\r\nGW : %d.%d.%d.%d", ip[0],ip[1],ip[2],ip[3]);
}



void Reset_W5200(void)
{

	GPIO_ResetBits(GPIOB, WIZ_RESET);
	wait_4us(2); // 
	GPIO_SetBits(GPIOB, WIZ_RESET);
	wait_10ms(15); // 

}


void LED3_onoff(uint8_t on_off)
{
	if (on_off == ON) {
		GPIO_ResetBits(GPIOA, LED3); // LED on
	}else {
		GPIO_SetBits(GPIOA, LED3); // LED off
	}
}

void LED4_onoff(uint8_t on_off)
{
	if (on_off == ON) {
		GPIO_ResetBits(GPIOA, LED4); // LED on
	}else {
		GPIO_SetBits(GPIOA, LED4); // LED off
	}
}

void USART1_Init(void)
{
USART_InitTypeDef USART_InitStructure;

/* USARTx configuration ------------------------------------------------------*/
  /* USARTx configured as follow:
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No ;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  
  /* Configure the USARTx */ 
  USART_Init(USART1, &USART_InitStructure);
  /* Enable the USARTx */
  USART_Cmd(USART1, ENABLE);


}


/*******************************************************************************
* Function Name  : Delay
* Description    : Inserts a delay time.
* Input          : nCount: specifies the delay time length.
* Output         : None
* Return         : None
*******************************************************************************/
// 4 usec delay
void Delay(uint32_t nCount)
{

	nCount = nCount*1000;
  for(; nCount != 0; nCount--);

}


void wait_10ms(uint32_t cnt)
{
	uint8_t i;
	
	for(i=0; i < 10; i++) wait_1ms(cnt);

}

void wait_1ms(uint32_t cnt)
{
	uint8_t i;
	
	for(i=0; i < 250; i++) wait_4us(cnt);
}

void wait_4us(uint32_t cnt)
{
	uint16_t i;
	for (i = 0; i < cnt; i++) Delay(1);

}


/*******************************************************************************
* Function Name  : PUTCHAR_PROTOTYPE
* Description    : Retargets the C library printf function to the USART.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
/*
PUTCHAR_PROTOTYPE
{
  // Write a character to the USART
  USART_SendData(USART1, (uint8_t) ch);

  //  Loop until the end of transmission 
  while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
  {
  }

  return ch;
}
*/

int putchar(int ch)
{
  // Write a character to the USART
  USART_SendData(USART1, (uint8_t) ch);

  //  Loop until the end of transmission 
  while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
  {
  }

  return ch;
}

uint8_t Get_Char(void)
{
uint16_t ch;

	while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET){
	}

	ch = USART_ReceiveData(USART1);

return ch;
}


#ifdef USE_FULL_ASSERT
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert_param error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert_param error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif



